package com.yinhai.message.manage.wxregister.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yinhai.core.app.ta3.web.controller.BaseController;
import com.yinhai.core.common.ta3.dto.TaParamDto;
import com.yinhai.message.implement.wx.tool.WxHttpsSendUtil;
import com.yinhai.message.manage.wxregister.bpo.IWxConfigBpo;
import com.yinhai.message.manage.wxregister.po.WxConfigPo;
import com.yinhai.message.manage.wxregister.vo.WxConfigVo;

/**
 * 微信公众号的配置
 * @author yanglq
 * 2017.5.2
 */
@RequestMapping("wxRegister")
@Controller
public class WxRegisterController extends BaseController{
	
	@Resource
	private IWxConfigBpo wxConfigBpo;
	/**
	 * 页面初始化
	 * @return
	 */
	@RequestMapping("wxRegisterController.do")
	public String execute() throws Exception{
		return "/manage/wxregister/wxregister";
	}
	
	@RequestMapping("wxRegisterController!toAddWx.do")
	public String toAddWx() throws Exception{
		return "/manage/wxregister/wxAdd";
	}
	
	@RequestMapping("wxRegisterController!toEditWx.do")
	public String toEditWx() throws Exception{
		TaParamDto dto = getTaDto();
		setData("id",dto.getAsString("id"));
		setData("appid",dto.getAsString("appid"));
		setData("secret",dto.getAsString("secret"));
		setData("name",dto.getAsString("name"));
		setData("defaultwx",dto.getAsString("defaultwx"));
		return "/manage/wxregister/wxEdit";
	} 
	
	@RequestMapping("wxRegisterController!removeWx.do")
	public String removeWx() throws Exception{
		TaParamDto dto = getTaDto();
		wxConfigBpo.removeWx(dto.getAsLong("id"));
		return JSON;
	}
	
	@RequestMapping("wxRegisterController!updateWx.do")
	public String updateWx() throws Exception{
		TaParamDto dto = getTaDto();
		WxConfigPo po = new WxConfigPo();
		po.setId(dto.getAsLong("id"));
		po.setAppid(dto.getAsString("appid"));
		po.setSecret(dto.getAsString("secret"));
		po.setName(dto.getAsString("name"));
		po.setDefaultwx(dto.getAsString("defaultwx"));
		wxConfigBpo.modifyWx(po);
		return JSON;
	}
	
	@RequestMapping("wxRegisterController!checkWx.do")
	public String checkWx() throws Exception{
		return JSON;
	}
	
	@RequestMapping("wxRegisterController!saveWx.do")
	public String saveWx() throws Exception{
		TaParamDto dto = getTaDto();
		String appid = dto.getAsString("appid");
		String secret = dto.getAsString("secret");
		String name = dto.getAsString("name");
		String defaultwx = dto.getAsString("defaultwx");
		WxHttpsSendUtil wxsend = WxHttpsSendUtil.getInstance();
		wxsend.getAccessToken(appid, secret);
		WxConfigPo po = new WxConfigPo();
		po.setAppid(appid);
		po.setSecret(secret);
		po.setName(name);
		po.setDefaultwx(defaultwx);
		wxConfigBpo.addWx(po);
		return JSON;
	}
	
	@RequestMapping("wxRegisterController!queryWx.do")
	public String queryWx() throws Exception{
		TaParamDto dto = getTaDto();
		String appid = dto.getAsString("appid");
		String name = dto.getAsString("name");
		List<WxConfigVo> list = wxConfigBpo.queryWxList(appid, name);
		setList("wxgrid",list);
		return JSON;
	}
}
